package funtastic.spellingbee;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

public class SpellingBeeActivity extends Activity
{
    private static final int IS_INSTALLED_CHECK_TTS_DATA = 1234;

    private Handler          mHandler;
    private SpellingBee      mSpellingBee;
    private TextView         mTextViewErrorMessage;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if (requestCode == IS_INSTALLED_CHECK_TTS_DATA)
        {
            if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS)
            {
                mSpellingBee.initializeTTS();
                mHandler.postDelayed(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        mTextViewErrorMessage.setText(mSpellingBee.getErrorMessage());
                    }
                }, 100L);
            }
            else
            {
                startActivity(new Intent(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA));
            }
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        Log.i(getString(R.string.namespace), "onCreate");
        
        super.onCreate(savedInstanceState);

        setContentView(R.layout.spellingbee_activity);

        mHandler = new Handler();
        mSpellingBee = (SpellingBee) getApplication();
        mTextViewErrorMessage = (TextView) findViewById(R.id.textViewErrorMessage);

        setTitle(mSpellingBee.getTitle());
        startActivityForResult(new Intent(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA), IS_INSTALLED_CHECK_TTS_DATA);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_options, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
            case R.id.itemSettings:
                startActivity(new Intent(this, SettingsActivity.class));
                return true;

            case R.id.itemCustom:
                startActivity(new Intent(this, CustomSpellingListActivity.class));
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    protected void onResume()
    {
        Log.i(getString(R.string.namespace), "onResume");
        super.onResume();

        setTitle(mSpellingBee.getTitle());
    }

    public void startExam(View view)
    {
        startActivity(new Intent(this, ExamActivity.class));
    }

    public void startPractice(View view)
    {
        startActivity(new Intent(this, PracticeActivity.class));
    }

    @Override
    protected void onDestroy()
    {
        Log.i(getString(R.string.namespace), "onDestroy");
        super.onDestroy();
    }

    @Override
    protected void onPause()
    {
        Log.i(getString(R.string.namespace), "onPause");
        super.onPause();
    }

    @Override
    protected void onRestart()
    {
        Log.i(getString(R.string.namespace), "onRestart");
        super.onRestart();
    }

    @Override
    protected void onStart()
    {
        Log.i(getString(R.string.namespace), "onStart");
        super.onStart();
    }

    @Override
    protected void onStop()
    {
        Log.i(getString(R.string.namespace), "onStop");
        super.onStop();
    }
}